<?php
namespace  app\common\model;

use think\Db;
use think\Model;
class Admin extends BaseModel {
    
    public function role()
    {
        return $this->hasOne('Role','id','role_id')->field('id,name,p_id,p_path');
        
    }

    /**
     * 用户登录
     */
    public  function login($user_name,$user_pwd){
        $info=   $this->where(["username"=>$user_name])->find();
        if(empty($info)){
            return false;
        }
  
        if(md5($user_pwd)== $info->userpwd){
            session("user_info", $info);
            session("user_id", $info->id);
            session("role_id", $info->role_id);
          
            $menu=model("Role")->menus($info->role_id);
            $member_id=$info->id;
            $user_role_item=model("Role")->where(["id"=>intval(session("role_id"))])->find();
            $allow_project=0;
            if(!empty($user_role_item)){
                $allow_project=$user_role_item->is_project;           
            }
            if($allow_project==0){
               $project_list= model("Team")->where(["user_id"=>  $member_id])->select()->toArray();
               $allow_project_ids= array_column($project_list,"project_id");           
               array_push($allow_project_ids,-1);
               session("allow_project_ids",$allow_project_ids);
            }else{
                session("allow_project_ids",999);
            }   
            session("user_menu", strtolower(serialize($menu)));
            return  true;
        }
        return false;
    }
   
}